package com.dominator.controller.api;

import com.dominFramework.core.asset.WebCxt;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.utils.JsonUtils;
import com.dominFramework.core.utils.SystemUtils;
import com.dominator.enums.ReqEnums;
import com.dominator.service.ApiOtosaasService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.api.ApiMessageUtil;
import com.dominator.utils.encode.MD5;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.network.IpUtils;
import com.dominator.utils.system.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping
@Slf4j
public class ApiOtosaasController {

    @Autowired
    HttpServletRequest request;



    @Autowired
    ApiOtosaasService  apiOtosaasService;

    @RequestMapping("/validation/v1")
    @ResponseBody
    public String validation() {
       String res=null;
        try {
            res=apiOtosaasService.validation(request);
        }  catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return res;
    }


    @PostMapping("/jointLogin")
    public void jointLogin(HttpServletResponse resp) {
        ApiMessage msg = new ApiMessage();
        try {
            Dto dto = JsonUtils.toDto(request.getParameter("paramStr"));
            dto.put("ip", IpUtils.getIpAddr(request));
            msg = apiOtosaasService.jointLogin(dto);
        } catch (ApiException e) {
            msg.setCode(e.getCode());
            msg.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error(e.getMessage());
            msg = new ApiMessage(ReqEnums.SYS_ERROR);
            e.printStackTrace();
        } finally {
            WebCxt.write(resp, msg);
        }

    }



}

